package edu.vital.energy.longevity.ch11.dao;

import edu.vital.energy.longevity.ch11.model.domain.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CommentMapper {
    // 分页展示某个文章的评论
    @Select("SELECT c.*, a.title AS articleTitle FROM t_comment c LEFT JOIN t_article a ON c.article_id = a.id WHERE c.article_id=#{aid} ORDER BY c.id DESC")
    List<Comment> selectCommentWithPage(Integer aid);

    // 后台查询最新几条评论
    @Select("SELECT c.*, a.title AS articleTitle FROM t_comment c LEFT JOIN t_article a ON c.article_id = a.id ORDER BY c.id DESC")
    List<Comment> selectNewComment();

    // 发表评论
    @Insert("INSERT INTO t_comment (article_id,created,author,ip,content) VALUES (#{articleId}, #{created},#{author},#{ip},#{content})")
    void pushComment(Comment comment);

    // 站点服务统计，统计评论数量
    @Select("SELECT COUNT(1) FROM t_comment")
    Integer countComment();

    // 通过文章id删除评论信息
    @Delete("DELETE FROM t_comment WHERE article_id=#{aid}")
    void deleteCommentWithId(Integer aid);

    // 根据评论ID删除评论
    @Delete("DELETE FROM t_comment WHERE id = #{id}")
    void deleteCommentById(Integer id);

    // 根据评论ID查询评论
    @Select("SELECT c.*, a.title AS articleTitle FROM t_comment c LEFT JOIN t_article a ON c.article_id = a.id WHERE c.id = #{id}")
    Comment selectCommentById(Integer id);

    // 更新评论内容
    @Update("UPDATE t_comment SET content = #{content} WHERE id = #{id}")
    void updateComment(Comment comment);

    // 使用MyBatis注解实现查询所有评论
    @Select("SELECT c.*, a.title AS articleTitle FROM t_comment c LEFT JOIN t_article a ON c.article_id = a.id ORDER BY c.created DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "articleId", column = "article_id"),
        @Result(property = "created", column = "created"),
        @Result(property = "author", column = "author"),
        @Result(property = "ip", column = "ip"),
        @Result(property = "content", column = "content"),
        @Result(property = "articleTitle", column = "articleTitle")
    })
    List<Comment> selectAllComments();
}